package com.servingcloud.servera.modules.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.servingcloud.servera.common.model.RestPage;
import com.servingcloud.servera.common.model.RestResult;
import com.servingcloud.servera.modules.entity.UserInfo;
import com.servingcloud.servera.modules.model.CompanyBO;
import com.servingcloud.servera.modules.model.UserInfoMODIFY;
import com.servingcloud.servera.modules.model.UserInfoQUERY;
import com.servingcloud.servera.modules.model.UserInfoSAVE;
import com.servingcloud.servera.modules.service.UserInfoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RefreshScope
@RequestMapping("/userInfo")
@Api(value="用户信息接口", tags="用户")
public class UserInfoController {
	@Value("${name}")
    private  String name;
    @Value("${age}")
    private  String age;
    @Value("${version}")
    private  String version="开发环境";
    @ApiIgnore //不显示该方法在api上
    @RequestMapping("/test1")
    public String test1(){
        return "a你好，我是"+name+",年龄："+age+"岁。当前环境："+version;
    }
	
    @Autowired
    private UserInfoService userInfoService;
    @Autowired HttpServletRequest request;
    private String getHostInfo() {
    	return request.getLocalName() +":"+request.getServerPort();
    }
    @ApiOperation(httpMethod="GET", value="获取服务器信息", notes="", response=String.class)
    @RequestMapping("/test")
    public String test(){
        return "我的地址："+getHostInfo();
    }
    @ApiOperation(httpMethod="POST", value="获取用户列表分页", notes="", response=UserInfo.class)
    @RequestMapping("/findUserInfoPage")
    public @ResponseBody RestResult<RestPage<UserInfo>> findUserInfoPage(@RequestBody UserInfoQUERY param){
    	RestResult<RestPage<UserInfo>> ret = new RestResult<RestPage<UserInfo>>();
    	ret.setSuccess(true);
    	ret.setErrorMsg(getHostInfo());
    	try {
    		ret.setReturnValue(userInfoService.findUserInfoPage(param));
    	}catch(Exception e) {
    		ret.setSuccess(false);
    		ret.setErrorCode("-1");
    		ret.setErrorMsg(e.getMessage());
    	}
    	return ret;
    }
    @ApiOperation(httpMethod="POST", value="新增用户信息", notes="", response=Long.class)
    @RequestMapping("/saveUserInfo")
    public @ResponseBody RestResult<Long> saveUserInfo(@RequestBody @Validated UserInfoSAVE param){
    	RestResult<Long> ret = new RestResult<Long>();
    	ret.setSuccess(true);
    	ret.setErrorMsg(getHostInfo());
    	try {
    		ret.setReturnValue(userInfoService.saveUserInfo(param));
    	}catch(Exception e) {
    		ret.setSuccess(false);
    		ret.setErrorCode("-1");
    		ret.setErrorMsg(e.getMessage());
    	}
    	return ret;
    }
    @ApiOperation(httpMethod="POST", value="修改用户信息", notes="")
    @RequestMapping("/modifyUserInfo")
    public @ResponseBody RestResult<Object> modifyUserInfo(@RequestBody @Validated UserInfoMODIFY param){
    	RestResult<Object> ret = new RestResult<Object>();
    	ret.setSuccess(true);
    	ret.setErrorMsg(getHostInfo());
    	try {
    		userInfoService.modifyUserInfo(param);
    	}catch(Exception e) {
    		ret.setSuccess(false);
    		ret.setErrorCode("-1");
    		ret.setErrorMsg(e.getMessage());
    	}
    	return ret;
    }
    @ApiOperation(httpMethod="POST", value="删除用户", notes="")
    @RequestMapping("/deleteUserInfo")
    public @ResponseBody RestResult<Object> deleteUserInfo(@RequestBody @Validated UserInfoMODIFY param){
    	RestResult<Object> ret = new RestResult<Object>();
    	ret.setSuccess(true);
    	ret.setErrorMsg(getHostInfo());
    	try {
    		userInfoService.deleteUserInfo(param);
    	}catch(Exception e) {
    		ret.setSuccess(false);
    		ret.setErrorCode("-1");
    		ret.setErrorMsg(e.getMessage());
    	}
    	return ret;
    }
    /*@ApiOperation(httpMethod="POST", value="获取公司列表", notes="")
    @RequestMapping("/findCompanyList")
    public @ResponseBody RestResult<RestPage<CompanyBO>> findCompanyList(@RequestBody UserInfoQUERY param){
    	RestResult<RestPage<CompanyBO>> ret = new RestResult<RestPage<CompanyBO>>();
    	ret.setSuccess(true);
    	try {
    		ret.setReturnValue(userInfoService.findCompanyList());
    	}catch(Exception e) {
    		e.printStackTrace();
    		ret.setSuccess(false);
    		ret.setErrorCode("-1");
    		ret.setErrorMsg(e.getMessage());
    	}
    	return ret;
    }*/
}
